class Settings {
    constructor(root) {
        this.root = root;
        this.platfrom = "WEB"
        if (this.root.AcWingOS) this.platfrom = "ACAPP";
        this.username = "";
        this.photo = "";
        this.$settings = $(`
        <div class="ac_game_settings">
            <div class="ac_game_settings_login">
               <div class="ac_game_settings_title">登录</div>
               <div class="ac_game_settings_username">
                    <div class="ac_game_settings_item">
                        <input type="text" placeholder="用户名">
                    </div>
               </div>
               <div class="ac_game_settings_password">
                    <div class="ac_game_settings_item">
                        <input type="password" placeholder="密码">
                    </div>
               </div>
               <div class="ac_game_settings_submit">
                    <div class="ac_game_settings_item">
                        <button>登录</button>
                    </div>
               </div>
               <div class="ac_game_settings_errorinfo"></div>
               <div class="ac_game_settings_option">注册</div>
               <div class="ac_game_settings_acwing">
                    <img src="https://cdn.acwing.com/media/article/image/2021/11/18/1_ea3d5e7448-logo64x64_2.png" alt="">
                    <br> 
                    <div>acwing一键登录</div>
               </div>
            </div>
            <div class="ac_game_settings_register">
               <div class="ac_game_settings_title">注册</div>
               <div class="ac_game_settings_username">
                    <div class="ac_game_settings_item">
                        <input type="text" placeholder="用户名">
                    </div>
               </div>
               <div class="ac_game_settings_password ac_game_settings_password_first">
                    <div class="ac_game_settings_item">
                        <input type="password" placeholder="密码">
                    </div>
               </div>
               <div class="ac_game_settings_password ac_game_settings_password_second">
                    <div class="ac_game_settings_item">
                        <input type="password" placeholder="确认密码">
                    </div>
               </div>
               <div class="ac_game_settings_submit">
                    <div class="ac_game_settings_item">
                        <button>注册</button>
                    </div>
               </div>
               <div class="ac_game_settings_errorinfo"></div>
               <div class="ac_game_settings_option">登录</div>
               <div class="ac_game_settings_acwing">
                    <img src="https://cdn.acwing.com/media/article/image/2021/11/18/1_ea3d5e7448-logo64x64_2.png" alt="">
                    <br> 
                    <div>acwing一键登录</div>
               </div>
            </div>
        </div>
        `);
        this.$login = this.$settings.find('.ac_game_settings_login');
        this.$login_username = this.$login.find('.ac_game_settings_username input');
        this.$login_password = this.$login.find('.ac_game_settings_password input');
        this.$login_submit = this.$login.find('.ac_game_settings_submit button');
        this.$login_register = this.$login.find('.ac_game_settings_option');
        this.$login_error_info = this.$login.find('.ac_game_settings_errorinfo')
        this.$login.hide();

        this.$register = this.$settings.find('.ac_game_settings_register');
        this.$register_username = this.$register.find('.ac_game_settings_username input');
        this.$register_password = this.$register.find('.ac_game_settings_password_first input');
        this.$register_psssword_confirm = this.$register.find('.ac_game_settings_password_second input');
        this.$register_submit = this.$register.find('.ac_game_settings_submit button');
        this.$register_login = this.$register.find('.ac_game_settings_option');
        this.$register_error_info = this.$register.find('.ac_game_settings_errorinfo');
        this.$register.hide();

        this.$acwing_login = this.$settings.find('.ac_game_settings_acwing img');
        this.root.$ac_game.append(this.$settings);
        this.start();
    }

    start() {
        if(this.platfrom === "ACAPP") {
            this.getinfo_acapp();
        } else {
            if (this.root.access) {
                this.getinfo_web();
                this.refresh_jwt_token();
            } else {
                this.login();
            }
            this.add_listening_event();
        }
    }

    refresh_jwt_token() {
        setInterval(() => {
            $.ajax({
                url: "https://app7030.acapp.acwing.com.cn/settings/token/refresh/",
                type: "post",
                data: {
                    refresh: this.root.refresh,
                },
                success: resp => {
                    console.log(resp);
                    this.root.access = resp.access;
                },
            });
        }, 1000);
    }


    add_listening_event() {
        let outer = this;
        this.add_listening_event_login();
        this.add_listening_event_register();
        this.$acwing_login.click(function() {
            outer.acwing_login();
        })
    }

    add_listening_event_login() {
        let outer = this;
        this.$login_register.click(function() {
            outer.register();
        });
        this.$login_submit.click(function() {
            outer.login_on_remote();
        });
    }

    add_listening_event_register() {
        let outer = this;
        this.$register_login.click(function() {
            outer.login();
        });
        this.$register_submit.click(function() {
            outer.register_on_remote();
        })
    }

    acwing_login() {
        $.ajax({
            url: "https://app7030.acapp.acwing.com.cn/settings/acwing/web/apply_code/",
            type: "GET",
            success: function(resp) {
                if (resp.result === "success") {
                    window.location.replace(resp.apply_code_url);
                }
            }
        })
    }

    login_on_remote(username, password) {

        username = username || this.$login_username.val();
        password = password || this.$login_password.val();


        this.$login_error_info.empty();
        $.ajax({
            url: "https://app7030.acapp.acwing.com.cn/settings/token/",
            type: "post",
            data: {
                username: username,
                password: password,
            },
            success: resp => {
                console.log(resp);
                this.root.access = resp.access;
                this.root.refresh = resp.refresh;
                this.refresh_jwt_token();
                this.getinfo_web();
            },
            error: () => {
                this.$login_error_info.html("用户名或密码错误");
            }
        })
    }

    register_on_remote() {
        let username = this.$register_username.val();
        let password = this.$register_password.val();
        let password_confirm = this.$register_psssword_confirm.val();
        this.$register_error_info.empty();
        $.ajax({
            url: "https://app7030.acapp.acwing.com.cn/settings/register/",
            type: "POST",
            data: {
                username: username,
                password: password,
                password_confirm: password_confirm,
            },
            success: resp => {
                if(resp.result === "success") {
                    this.login_on_remote(username, password);
                } else {
                    this.$register_error_info.html(resp.result);
                }
            }
        })
    }

    logout_on_remote() {
        if (this.platfrom === "ACAPP") {
            this.root.AcWingOS.api.window.close();
        } else {
            this.root.access = "";
            this.root.refresh = "";
            location.href = "/";
        }
    }

    login() {
        this.$register.hide();
        this.$login.show();
    }

    register() {
        this.$login.hide();
        this.$register.show();
    }

    getinfo_web() {
        $.ajax({
            url: "https://app7030.acapp.acwing.com.cn/settings/getinfo/",
            type: "GET",
            data: {
                platfrom: this.platfrom,
            },
            headers: {
                'Authorization': "Bearer " + this.root.access,
            },
            success: resp => {
                if(resp.result === "success") {
                    console.log(resp);
                    this.username = resp.username;
                    this.photo = resp.photo;
                    this.hide();
                    this.root.menu.show();
                }else {
                    this.login();
                }
            }
        })
    }

    acapp_login(appid, redirect_uri, scope, state) {
        this.root.AcWingOS.api.oauth2.authorize(appid, redirect_uri, scope, state, resp => {
            if(resp.result === "success") {
                this.username = resp.username;
                this.photo = resp.photo;
                this.hide();
                this.root.menu.show();
                this.root.access = resp.access;
                this.root.refresh = resp.refresh;
                this.refresh_jwt_token();
            }
        });
    }

    getinfo_acapp() {
        let outer = this;
        $.ajax({
            url: "https://app7030.acapp.acwing.com.cn/settings/acwing/acapp/apply_code/",
            type: "GET",
            success: function(resp) {
                if(resp.result === "success") {
                    outer.acapp_login(resp.appid, resp.redirect_uri, resp.scope, resp.state);
                }
            }
        })
    }

    hide() {
        this.$settings.hide();
    }

    show() {
        this.$settings.show();
    }
}
